package com.jt.common.thread;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Synclests {
    private static List<String> cache = new ArrayList<>();
    //private static List<String> cache = new CopyOnWriteArrayList<>();
    public static List<String> selectAll(){
        if(cache.isEmpty()){
            System.out.println("Get ");
            List<String> cate = Arrays.asList("a","b","c");
            cache.addAll(cate);
        }
        return cache;
    }
    /*public static List<String> selectAll(){
        if(cache.isEmpty()) {//A,B,C,D
            synchronized (cache) {
                if (cache.isEmpty()) {
                    System.out.println("Get Data From Database");
                    List<String> data = Arrays.asList("A", "B", "C");
                    cache.addAll(data);
                }
            }
        }
        return cache;
    }*/
    public static void main(String[] args) {
        //System.out.println(Thread.currentThread().getName());
        Thread t1=new Thread(){
            @Override
            public void run() {
                System.out.println(selectAll());
            }
        };
        Thread t2=new Thread(){
            @Override
            public void run() {
                System.out.println(selectAll());
            }
        };
        Thread t3=new Thread(){
            @Override
            public void run() {
                System.out.println(selectAll());
            }
        };
        Thread t4=new Thread(){
            @Override
            public void run() {
                System.out.println(selectAll());
            }
        };
        Thread t5=new Thread(){
            @Override
            public void run() {
                System.out.println(selectAll());
            }
        };
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();


    }
}
